package edu.vt.ece.rt.benchmark.vacation;

import java.util.ArrayList; 

import edu.vt.ece.rt.dstm.QObject;
import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.Resource;

public class Customer extends QObject {

	String id;
	ArrayList<Reservation> reservations;
	
	
	public Customer(String ID) {
		id = ID;
		reservations = new ArrayList<Reservation>();
		Resource.putQObject(getId(), this);
	}
	
	public Customer() {
		reservations = new ArrayList<Reservation>();	
	}

	@Override
	public QObject clone() {
		Customer c = new Customer();
		c.setId(getId());
		c.setReservations(getReservations());
		c.setCloneOwner(getCloneOwner());
		c.setVersion(getVersion());
		return c;
	}

	@Override
	public String getId() {
		return id;
	}
	
	public void setId(String newId) {
		id = newId;
	}

	public ArrayList<Reservation> getReservations() {
		return reservations;
	}
	
	public void setReservations(ArrayList<Reservation> arr) {
		
		for (int i = 0 ; i < arr.size(); i++) {
			getReservations().add(arr.get(i));
		}
	}
	
	public void addReservations(Reservation r) {
		getReservations().add(r);
	} 
	
	public void checkReservations() {
		for (int i = 0 ; i < getReservations().size(); i++) {
			// do nothing for read only operation
		}
	
	}

	public boolean addReservation(Travel travelItem) {
		
		int reservationNum = Resource.counter.getAndIncrement();
		String reservationId = ClustersManager.baseClusterId +"-"+reservationNum;
		if (getReservations().contains(reservationId)) {
			return false;
		}
		
		Reservation r =  new Reservation(reservationId, travelItem.getId(), travelItem.getPrice());
		addReservations(r);
		return true;
	}
	
	public void checkSanity(){
		int size = getReservations().size();
		for (int i = 0 ; i < size; i++) {
			Reservation r = getReservations().remove(0);
			for (int j = 0 ; j < getReservations().size(); j++) {
				if (r.getId().equals(getReservations().get(j).getId())) {
					System.out.println("Error found for "+getId());
				}
			}
		}
	}
}
